define(function (require, exports, module) {
	var store = require("store");
	var activeUser = store.get("user");
	var aw = require("ajaxwrapper");
	var planfile = require("../store/planfile");
	var Dialog = require("dialog");
	
	module.exports = {
			grid:{
				appointment:function(widget,data,rowEle){
					widget.set("check",false);
					widget.hide([".J-grid"]).show([".J-form",".J-editgrid"]);
					widget.get("subnav").hide(["add","search","servicePoint","checkInStatus","reserveDate"]).show(["save","return"]);
					widget.get("form").setData({
						pkCIImplement : data.pkCIImplement,
						groupUnit : data.groupReserve.planItemGroup.alliance?data.groupReserve.planItemGroup.allianceOrg.name
									:data.groupReserve.operator.servicePoint.name,
						destination : data.groupReserve.planItemGroup.planItem.destination.name,
						signUpNumber : data.roomPersonalInfos.length,
						checkInDate : data.groupReserve.checkInDate,
						checkOutDate : data.groupReserve.checkOutDate,
						checkInType : data.roomPersonalInfos[0].roomRegister.checkInType,
						reservePerson : data.groupReserve.operator.name,
						roomType : data.groupReserve.roomRegisters[0].room.roomType.name,
						roomPrice : data.vipPrice,
						checkInPrice : data.roomPersonalInfos[0].roomRegister.checkInPrice,
						reserveDate : data.groupReserve.reserveDate,
					});
					widget.set("pkCIImplement",data.pkCIImplement);
					widget.get("optiongrid").refresh();
					setTimeout(function(){
						$(".J-grid-columns-roomRegister-checkInPrice").eq(0).click();
					},300);
					
				},
				check:function(widget,data,rowEle){
					widget.set("check",true);
					widget.hide([".J-grid"]).show([".J-form",".J-editgrid"]);
					widget.get("subnav").hide(["add","search","servicePoint","checkInStatus","reserveDate"]).show(["return"]);
					widget.get("form").setData({
						pkCIImplement : data.pkCIImplement,
						groupUnit : data.groupReserve.planItemGroup.alliance?data.groupReserve.planItemGroup.allianceOrg.name
									:data.groupReserve.operator.servicePoint.name,
						destination : data.groupReserve.planItemGroup.planItem.destination.name,
						signUpNumber : data.roomPersonalInfos.length,
						checkInDate : data.groupReserve.checkInDate,
						checkOutDate : data.groupReserve.checkOutDate,
						checkInType : data.roomPersonalInfos[0].roomRegister.checkInType,
						reservePerson : data.groupReserve.operator.name,
						roomType : data.groupReserve.roomRegisters[0].room.roomType.name,
						roomPrice : data.vipPrice,
						checkInPrice : data.roomPersonalInfos[0].roomRegister.checkInPrice,
						reserveDate : data.groupReserve.reserveDate,
					});
					widget.set("pkCIImplement",data.pkCIImplement);
					widget.get("optiongrid").refresh();
				},
			},
			subnav:{
				saveAction:function(widget){
					var optiongridData=widget.get("optiongrid").getData();
					var data=widget.get("form").getData();
					var dd={};
					var saveData={};
					var dat={};
					
					dd.pkCIImplement=data.pkCIImplement;
					for(var i in optiongridData){
						optiongridData[i].signUpRegister.personalInfo.sex=optiongridData[i].signUpRegister.personalInfo.sex.key;
						optiongridData[i].roomRegister.room.status = optiongridData[i].roomRegister.room.status.key;
						optiongridData[i].roomRegister.roomStatus = optiongridData[i].roomRegister.roomStatus.key;
						optiongridData[i].roomRegister.status = optiongridData[i].roomRegister.status.key;
						optiongridData[i].roomRegister.checkInType = null;
						optiongridData[i].checkInImplement.confirmStatus = optiongridData[i].checkInImplement.confirmStatus.key;
						optiongridData[i].checkInImplement.dataSource = optiongridData[i].checkInImplement.dataSource.key;
						optiongridData[i].checkInImplement.roomStatus = optiongridData[i].checkInImplement.roomStatus.key;
						optiongridData[i].checkInImplement.statementStatus = optiongridData[i].checkInImplement.statementStatus.key;
						optiongridData[i].checkInImplement.status = optiongridData[i].checkInImplement.status.key;
						if(typeof(optiongridData[i].signUpRegister.travelContract) != "string" && optiongridData[i].signUpRegister.travelContract){
							optiongridData[i].signUpRegister.travelContract.unit = optiongridData[i].signUpRegister.travelContract.unit.key;
						}
						if(optiongridData[i].checkIn==false){
							Dialog.alert({
								  title : "提示",
								  content : "还没有全部登记",
								});
							return;
						}
					}
					if(!data.checkInType){
						Dialog.alert({
							title : "提示",
							content : "入住类型不能为空",
						});
						return;
					}
					saveData.checkInType = data.checkInType;
					saveData.checkInImplement=dd;
					saveData.temp=optiongridData;
					saveData.operator = activeUser.pkUser;
	            	aw.saveOrUpdate("api/checkinimplement/savecheckin", aw.customParam(saveData), function(datas){
	            		widget.show([".J-grid"]).hide([".J-form",".J-editgrid"]);
						widget.get(/*"add",*/"subnav").show(["search","servicePoint","checkInStatus","reserveDate"]).hide(["save","return"]);
						widget.get("grid").refresh();
	            	});
					
					
				},
				reback:function(widget){
					widget.show([".J-grid"]).hide([".J-form",".J-editgrid",".J-signupgrid",".J-signupform"]);
					widget.get(/*"add",*/"subnav").show(["search","servicePoint","checkInStatus","reserveDate"]).hide(["newsave", "save","return"]);
					widget.get("grid").refresh();
				}
			},
			optiongrid:{
				appointment:function(widget,data){
					var checkInPrice = data.roomRegister.checkInPrice;
					if(data.signUpRegister.travelContract && data.signUpRegister.travelContract.unit.key == "Day"){
						var travelContract = data.signUpRegister.travelContract;
						checkInPrice = travelContract.price/travelContract.balTime;
					}
					if(data.roomRegister.room == null){
						Dialog.alert({
							tip: "提示！",
							content: "该团员未分配房间，请分配房间后再进行登记！"
						});
						return false;
					}
					if(data.roomRegister.room.status.key == "Dirty"
						|| data.roomRegister.room.status.key == "Repairing"){
						
						Dialog.confirm({
							tip: "提示",
							content: "此房为脏房状态或是维修状态，是否继续办理？",
							confirm: function(){
								var breakfast = false;
								var waterElectric = false;
								if(data.roomRegister.breakfast!=null){
									if(typeof data.roomRegister.breakfast == "object"){
										if(data.roomRegister.breakfast.key==true || data.roomRegister.breakfast.key=="true"){
											breakfast = true;
										}else{
											breakfast = false;
										}
									}else{
										if(data.roomRegister.breakfast==true || data.roomRegister.breakfast=="true"){
											breakfast = true;
										}else{
											breakfast = false;
										}
									}
								}
								if(data.roomRegister.waterElectric!=null){
									if(typeof data.roomRegister.waterElectric == "object"){
										if(data.roomRegister.waterElectric.key==true || data.roomRegister.waterElectric.key=="true"){
												waterElectric = true;
										}else{
												waterElectric = false;
										}
									}else{
										if(data.roomRegister.waterElectric==true || data.roomRegister.waterElectric=="true"){
											waterElectric = true;
										}else{
											waterElectric = false;
										}
									}
								}
								var saveData = {
										"pkRoomPersonalInfo" : data.pkRoomPersonalInfo,
										"breakfast":breakfast,
										"waterElectric":waterElectric,
										"checkIn":true,
										"checkInPrice":checkInPrice,
								
								};
								aw.saveOrUpdate("api/roompersonalInfo/saveappointment", aw.customParam(saveData), function(datas){
									widget.get("optiongrid").refresh();
								});
							}
						})
					}else{
						var breakfast = false;
						var waterElectric = false;
						if(data.roomRegister.breakfast!=null){
							if(typeof data.roomRegister.breakfast == "object"){
								if(data.roomRegister.breakfast.key==true || data.roomRegister.breakfast.key=="true"){
									breakfast = true;
								}else{
									breakfast = false;
								}
							}else{
								if(data.roomRegister.breakfast==true || data.roomRegister.breakfast=="true"){
									breakfast = true;
								}else{
									breakfast = false;
								}
							}
						}
						if(data.roomRegister.waterElectric!=null){
							if(typeof data.roomRegister.waterElectric == "object"){
								if(data.roomRegister.waterElectric.key==true || data.roomRegister.waterElectric.key=="true"){
										waterElectric = true;
								}else{
										waterElectric = false;
								}
							}else{
								if(data.roomRegister.waterElectric==true || data.roomRegister.waterElectric=="true"){
									waterElectric = true;
								}else{
									waterElectric = false;
								}
							}
						}
						var saveData = {
								"pkRoomPersonalInfo" : data.pkRoomPersonalInfo,
								"breakfast":breakfast,
								"waterElectric":waterElectric,
								"checkIn":true,
								"checkInPrice":checkInPrice,
						};
						aw.saveOrUpdate("api/roompersonalInfo/saveappointment", aw.customParam(saveData), function(datas){
							widget.get("optiongrid").refresh();
						});
					}
				},
				cancel:function(widget,data){
					var saveData = {
	            			"pkRoomPersonalInfo" : data.pkRoomPersonalInfo,
	            			"breakfast":false,
							"waterElectric":false,
	            			"checkIn":false,
	            	};
	            	aw.saveOrUpdate("api/roompersonalInfo/savecancel", aw.customParam(saveData), function(datas){
	            		widget.get("optiongrid").refresh();
	            	});
				}
			},
			addSave:function(widget){
				var signupgrid = widget.get("signupgrid");
				var subnav=widget.get("subnav");
				
				var roomRegisterArr = [];
				for(var i=0;i<signupgrid.getData().length;i++){
					if(signupgrid.getData()[i].roomRegister.room){
						roomRegisterArr.push(signupgrid.getData()[i].roomRegister)	
					}
				}
				
				aw.saveOrUpdate("api/roomregister/savenew",aw.customParam({roomRegisterList:roomRegisterArr}),function(data){
					widget.get("grid").refresh();
					widget.hide([".J-editgrid",".J-form",".J-editform",".J-signupform",".J-signupgrid"]).show([".J-grid"]);
					widget.get("subnav").hide(["return","newsave","add"]).show(["servicePoint","checkInStatus","reserveDate"]);
				});
				
			}
	};
});